@namespace Oqtane.Modules.Controls
@inherits ModuleControlBase
@inject NavigationManager NavigationManager

@if (!string.IsNullOrEmpty(Message))
{
    @if (_style == MessageStyle.Alert)
    {
        <div class="@_classname alert-dismissible fade show mb-3" role="alert">
            @((MarkupString)Message)
            @if (Type == MessageType.Error && PageState != null && UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
            {
                <NavLink class="ms-2" href="@NavigateUrl("admin/log")">View Details</NavLink>
            }
            <form method="post" class="app-form-inline" @formname="ModuleMessageForm" @onsubmit="CloseMessage" data-enhance>
                <input type="hidden" name="@Constants.RequestVerificationToken" value="@SiteState.AntiForgeryToken" />
                <button type="submit" class="btn-close" data-dismiss="alert" aria-label="close"></button>
            </form>
        </div>
    }

    @if (_style == MessageStyle.Toast)
    {
        <div class="app-modulemessage-toast bottom-0 end-0" @key="DateTime.UtcNow">
            <div class="@_classname alert-dismissible fade show mb-3 rounded-end-0" role="alert">
                @((MarkupString)Message)
                @if (Type == MessageType.Error && PageState != null && UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
                {
                    <NavLink class="ms-2" href="@NavigateUrl("admin/log")">View Details</NavLink>
                }
                <form method="post" class="app-form-inline" @formname="ModuleMessageForm" @onsubmit="CloseMessage" data-enhance>
                    <input type="hidden" name="@Constants.RequestVerificationToken" value="@SiteState.AntiForgeryToken" />
                    <button type="submit" class="btn-close" data-dismiss="alert" aria-label="close"></button>
                </form>
            </div>
        </div>
    }
}

@code {
    private string _message = string.Empty;
    private string _classname = string.Empty;
    private MessageStyle _style;

    [Parameter]
    public string Message { get; set; }

    [Parameter]
    public MessageType Type { get; set; }

    [Parameter]
    public MessageStyle Style { get; set; } = MessageStyle.Alert;

    [Parameter]
    public RenderModeBoundary Parent { get; set; }

    protected override void OnParametersSet()
    {
        _message = Message;
        if (!string.IsNullOrEmpty(_message))
        {
            _classname = GetMessageType(Type);
            _style = Style;
            if (Type == MessageType.Error)
            {
                _style = MessageStyle.Alert; // errors should always be displayed as alerts
            }
        }
    }

    private string GetMessageType(MessageType type)
    {
        string classname = string.Empty;
        switch (type)
        {
            case MessageType.Success:
                classname = "alert alert-success";
                break;
            case MessageType.Info:
                classname = "alert alert-info";
                break;
            case MessageType.Warning:
                classname = "alert alert-warning";
                break;
            case MessageType.Error:
                classname = "alert alert-danger";
                break;
        }

        return classname;
    }

    private void CloseMessage()
    {
        if (Parent != null)
        {
            Parent.DismissMessage();
        }
        else
        {
            NavigationManager.NavigateTo(NavigationManager.Uri);
        }
    }
}
